#include "paintingclass.h"

extern btMusicBox music;
String last_title = "";
String last_artist = "";
uint8_t flag_first = 0;
uint8_t music_lyrics[100] = {0};
uint8_t music_lyrics1[100] = {0};

std::chrono::steady_clock::time_point _audioUpdate;

paintingclass* director = nullptr;

//必须要实现的dot2d导演对象代理方法
class MainDelegate : public dot2d::DirectorDelegate
{

    void render()
    {
        FastLED.show();
        // 输出ESP32内存占用情况
        // Serial.printf("-----Free Heap Mem : %d [%.2f%%]-----\n",
        //         ESP.getFreeHeap(),
        //         ESP.getFreeHeap()/(double)ESP.getHeapSize()*100);
        // Serial.printf("-----Free PSRAM Mem: %d [%.2f%%]-----\n",
        //         ESP.getFreePsram(),
        //         ESP.getFreePsram()/(double)ESP.getPsramSize()*100);
    }

    size_t write(uint8_t c)
    {
      return Serial.write(c);
    }

    //返回一个RGB对象的顺序表指针，用于初始化硬件屏幕
    void initMatrix(dot2d::DTRGB *data)
    {
        FastLED.addLeds<WS2812Controller800Khz,MATRIX_LED_PIN, GRB>((CRGB* )data,MATRIX_WIDTH*MATRIX_HEIGHT,0);
        FastLED.setBrightness(MATRIX_MAX_BRIGHTNESS); //设置WS2812屏幕亮度
    }

    //用于计算坐标为(x,y)的灯珠在RGB对象顺序表中的具体位置，适配不同排列方式的屏幕，在此处修改
    uint32_t dotOrder(uint16_t x,uint16_t y)
    {
      //uint16_t order = y*MATRIX_WIDTH;

      // if ((y%2)==0)
      // {
      //     order+=x;
      // }else
      // {
      //     order+=(MATRIX_WIDTH-x-1);
      // }
      //  uint16_t order = x*MATRIX_HEIGHT;

      //  order+=(MATRIX_HEIGHT-y-1);
       uint16_t order = x*MATRIX_HEIGHT;

       order += y;

       return order;
    }
};

void attendpaintingclass(void)
{
    painting = paintingclass::getInstance();                      //获取导演对象
    painting->setDelegate(new MainDelegate());                      //设置导演代理
    painting->setFramesPerSecond(30);                               //设置帧速率
    painting->initDotCanvas(MATRIX_WIDTH,MATRIX_HEIGHT);            //初始化导演画布

    painting->runWithScene(dot2d::MusicLyrics::create());
}

void audioLyricsupdate(uint16_t duration)
{
    int gbk_len = 0;
    auto now = std::chrono::steady_clock::now();
    int64_t dt = std::chrono::duration_cast<std::chrono::milliseconds>(now - _audioUpdate).count();
    if(dt >= duration)
    {
        _audioUpdate = now;
        music.updateMeta();
        if(music.artist.compareTo(last_artist) != 0){
            // Serial.print("Artist: ");
            // Serial.println(audio.artist);
            // Serial.print("Album: ");
            // Serial.println(audio.album);
            // Serial.print("Genre: ");
            // Serial.println(audio.genre);
            last_artist = music.artist;
        }
        if(music.title.compareTo(last_title)!=0){
            // Serial.print("Title: ");
            Serial.println(music.title);
            if(-1 != SwitchToGbk((const uint8_t *)music.title.c_str(),strlen(music.title.c_str()),music_lyrics1,&gbk_len)){
                memcpy(music_lyrics,music_lyrics1,sizeof(music_lyrics1));
            }
            last_title = music.title;
        }
    }
}
